import locale
import sys
from aiohttp import web
import os
import aiohttp_cors
from dicom_viewer_be_mock_xiaosai.utils.logger import logger
from src.dicom_viewer_be_mock_xiaosai.service.socketio_service import sio
from src.dicom_viewer_be_mock_xiaosai.share import machine_config

# 设置默认编码
# 不加上这个aio http响应会是Unicode 转义序列格式的响应。
# if sys.platform.startswith('win'):
#     locale.setlocale(locale.LC_ALL, 'zh_CN.UTF-8')

def run_http():
    from src.dicom_viewer_be_mock_xiaosai.service.http_service import app
    cors = aiohttp_cors.setup(app, defaults={
        "*": aiohttp_cors.ResourceOptions(
            allow_credentials=True,
            expose_headers="*",
            allow_headers="*",
        )
    })
    # 先添加自带的路由，再添加sio，sio自带了cors。
    for route in list(app.router.routes()):
        cors.add(route)
    sio.attach(app)
    web.run_app(app, host='0.0.0.0', port=8080)


def device_detector():
    # import torch
    # machine_config["gpu"] =  torch.cuda.is_available()
    machine_config["cpu_count"] =  os.cpu_count()
    logger.info(f"gpu是否可用：{machine_config["gpu"]},cpu_数量为:{machine_config["cpu_count"]}")



# 主函数
if __name__ == '__main__':
    device_detector()
    run_http()